package com.spzx.channel.service.impl;

import com.spzx.channel.service.ChannelService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.SpzxCache;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteSkuOrBrandService;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.api.domain.Category;
import com.spzx.product.api.domain.vo.ProductSkuBrandVo;
import com.spzx.product.api.domain.vo.SkuDetailVo;
import com.spzx.product.api.domain.vo.SkuSearchVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChannelServiceImpl implements ChannelService {
    @Resource
    private RemoteSkuOrBrandService remoteSkuOrBrandService;
    @Resource
    private RemoteCategoryService remoteCategoryService;

    //    @Cacheable(value = "spzx-channel-index",key = "'hotData'")
    @Override
    @SpzxCache(cachePrefix = "spzx-channel-hotData")
    public ProductSkuBrandVo indexHotDate() {
        R<ProductSkuBrandVo> skuOrBrand = remoteSkuOrBrandService.getSkuOrBrand();
        if (skuOrBrand.getCode() != 200) {
            throw new ServiceException(skuOrBrand.getMsg());
        }
        return skuOrBrand.getData();
    }

    //    @Cacheable(value = "spzx-channel",key = "'categoryTree'")
    @Override
    @SpzxCache(cachePrefix = "spzx-channel-categoryTree")
    public List<Category> getCategoryTree() {
        R<List<Category>> categoryTree = remoteCategoryService.listCategoryTree();
        if (categoryTree.getCode() != 200) {
            throw new ServiceException(categoryTree.getMsg());
        }
        return categoryTree.getData();
    }

//    @Cacheable(value = "spzx-channel-sku", key = "#skuSearchVo.toString()")
    @Override
    @SpzxCache(cachePrefix = "spzx-channel-skuList")
    public TableDataInfo getSkuList(SkuSearchVo skuSearchVo) {
        R<TableDataInfo> skuList = remoteSkuOrBrandService.getSkuList(skuSearchVo);
        if (skuList.getCode() != 200) {
            throw new ServiceException(skuList.getMsg());
        }
        return skuList.getData();
    }

//    @Cacheable(value = "spzx-channel", key = "'brand'")
    @Override
    @SpzxCache(cachePrefix = "spzx-channel-brand")
    public List<Brand> getBrandAll() {
        R<List<Brand>> brandAll = remoteSkuOrBrandService.getBrandAll();
        if (brandAll.getCode() != 200) {
            throw new ServiceException(brandAll.getMsg());
        }
        return brandAll.getData();
    }

//    @Cacheable(value = "spzx-channel", key = "'skuDetail'")
    @Override
    @SpzxCache(cachePrefix = "spzx-channel-skuDetail")
    public SkuDetailVo getSkuDetail(Long skuId) {
        R<SkuDetailVo> skuDetail = remoteSkuOrBrandService.getSkuDetail(skuId);
        if (skuDetail.getCode() != 200) {
            throw new ServiceException(skuDetail.getMsg());
        }
        return skuDetail.getData();
    }
}
